package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.CityMode;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.contract.SearchContract;
import com.sdcqjy.property.presenter.contract.a;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 磁磁帅 on 2018/4/10.
 * 功能：
 */
public class SearchPresenter extends BaseContract.Presenter<SearchContract.View> {
    public SearchPresenter(SearchContract.View baseView) {
        super(baseView);
    }

    private String keyword;

    public String getKeyword() {
        return keyword;
    }

    public void searchList(String keyword) {
        this.keyword = keyword;
        Map<String, Object> map = new HashMap<>();
        map.put("categoryIDs", "1,2,5");//1,2,5
        map.put("documentName", keyword);
        CityMode mode = CityMode.getMode(AppLL.getAppLL());
        map.put("projectID", mode.id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetWordList, new BaseRequestBack<BaseRet<List<ListMsgMode>>>() {
                    public BaseRet<List<ListMsgMode>> jsonToMode(String jsonStr) {
                        BaseRet<List<ListMsgMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<ListMsgMode>>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<List<ListMsgMode>> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.searchListRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }
}
